using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Activities.Rules;
using System.Xml;
using System.Reflection;
using NVelocityTemplateEngine.Interfaces;
using System.Collections;
using NVelocityTemplateEngine;

namespace EventAbstractionAPI.RuleEngine
{
	public class OrEvent:IComplexEvent
	{
        public OrEvent(TimeSpan timeToLive,String complexEvent, params String[] events)
        {
            _timeToLive = timeToLive;
            _complexEventName = complexEvent;
            _events = events;
        }

        #region ComplexEvent Members

        public XmlDocument Rule
        {
            get
            {
                string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;

                INVelocityEngine embeddedEngine = NVelocityEngineFactory.CreateNVelocityAssemblyEngine(assemblyName, "EventAbstractionAPI.RuleEngine", true);

                IDictionary context = new Hashtable();

                context.Add("ComplexEvent", _complexEventName);
                context.Add("Name", _complexEventName);

                int count = 1;
                foreach (String eventType in _events)
                {
                    context.Add("EventType" + count.ToString(), eventType);
                    count++;
                }

                String xmlString = embeddedEngine.Process(context, "Resources.OrRule.vm");

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlString);

                return doc;
            }
            set{throw new Exception("The method or operation is not implemented.");}
        }

        public TimeSpan TimeOfLife
        {
            get
            {
                return _timeToLive;
            }
            set
            {
                _timeToLive = value;
            }
        }

        public string FirstParameter
        {
            get
            {
                return _events[0];
            }
        }

        #endregion

        #region Member Variables

        TimeSpan _timeToLive;
        String _complexEventName;
        String[] _events;

        #endregion
    }
}
